package com.gdk.ksoft.assembly.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.utils.extensions.ControlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by hel on 2014-12-08.
 * 修改车销商品的信息的提示框
 */
public class FixCxqhDialog extends Dialog {

    public interface OnValueChangedListener{
        public void onPriceChange(int js,int ss);
    }

    @ViewInject(R.id.dialog_fixCxqh_etJs)private EditText etJs;//件数输入框
    @ViewInject(R.id.dialog_fixCxqh_etSs)private EditText etSs;//件数输入框
    private OnValueChangedListener listener;//监听事件
    private int js = 0,ss = 0,hsgx = 1;//商品的件数、散数、换算关系

    /**
     * 构造函数
     * @param context
     * @param js 件数数量
     * @param ss 散数数量
     * @param hsgx 换算关系
     * @param listener
     */
    public FixCxqhDialog(Context context, int js, int ss, int hsgx, OnValueChangedListener listener) {
        super(context,R.style.WindowDialogTheme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_fix_cxqh,null);
        setContentView(rootView);
        ViewUtils.inject(this, rootView);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
        this.listener = listener;
        this.js = js;
        this.ss = ss;
        this.hsgx = hsgx;
        etJs.setText(String.valueOf(js));//设置件数
        etSs.setText((ss == 0?"":String.valueOf(ss)));//设置散数
    }

    @OnClick(value = {R.id.dialog_fixCxqh_txtOk,R.id.dialog_fixCxqh_txtCancel})
    public void OnViewClick(View view){
        switch(view.getId()){
            case R.id.dialog_fixCxqh_txtOk://确定按钮
                String jsStr = ControlUtils.getNumberStringFromTextView(etJs);
                String ssStr = ControlUtils.getNumberStringFromTextView(etSs);
                int intJs = Integer.parseInt(jsStr);
                int intSs = Integer.parseInt(ssStr);
                if((intJs == 0 && intSs == 0)||(intJs>0 && intSs < 0) || (intJs<0 && intSs>0)){
                    ControlUtils.showToast(getContext(),"请输入正确的数量!",Gravity.CENTER,Toast.LENGTH_SHORT);
                    return;
                }
                if(Math.abs(intSs)>=hsgx){
                    ControlUtils.showToast(getContext(),"散数数量大于等于规格："+hsgx,Gravity.CENTER,Toast.LENGTH_SHORT);
                    return;
                }
                if(listener!=null){
                    listener.onPriceChange(intJs,intSs);
                }
                this.dismiss();
                break;
            case R.id.dialog_fixCxqh_txtCancel://取消按钮
                this.dismiss();
                break;
        }
    }

}
